using HybridCLR.Editor.Commands;
using System.Linq;
using UnityEditor;
using UnityEngine;

namespace HybridCLR.Editor
{
    public class BuildPlayerCommand
    {

        [Tooltip("打包可执行程序,不对现有设置进行修改，直接打包")]
        [MenuItem("Build/PC Standalone")]
        public static void Build_Standalone()
        {
            BuildTarget target = BuildTarget.StandaloneWindows64;
#if UNITY_EDITOR_LINUX
            target = BuildTarget.StandaloneLinux64;
#endif
            BuildTarget activeTarget = EditorUserBuildSettings.activeBuildTarget;
            if (activeTarget != target)
            {
                Debug.LogError($"请先切到{target}平台再打包");
                return;
            }
            // Get filename.
            string outputPath = $"{SettingsUtil.ProjectDir}/Build/{activeTarget}";

            var buildOptions = BuildOptions.CompressWithLz4;
            var productName = PlayerSettings.productName;

            string location = $"{outputPath}/{productName}{GetTargetExt(target)}";

            Debug.Log("====> Build App");
            BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions()
            {
                scenes = EditorBuildSettings.scenes.Select(f=>f.path).ToArray(),
                locationPathName = location,
                options = buildOptions,
                target = target,
                targetGroup = BuildTargetGroup.Standalone,
            };

            var report = BuildPipeline.BuildPlayer(buildPlayerOptions);
            if (report.summary.result != UnityEditor.Build.Reporting.BuildResult.Succeeded)
            {
                Debug.LogError("打包失败");
                return;
            }
#if UNITY_EDITOR
            Application.OpenURL($"file:///{location}");
#endif
        }

        static string GetTargetExt(BuildTarget target)
        {
            if (target == BuildTarget.StandaloneWindows || target == BuildTarget.StandaloneWindows64)
                return ".exe";
            if (target == BuildTarget.EmbeddedLinux || target == BuildTarget.StandaloneLinux64)
                return ".run";
            return "";
        }
        
        [Tooltip("打包可执行程序,不使用HybridCLR热更；先打包yooasset热更bundle,最后打包可执行程序")]
        [MenuItem("Build/UnityPlayer")]
        public static void Build_UnityPlayer()
        {
            BuildTarget activeTarget = EditorUserBuildSettings.activeBuildTarget;
            
            // Get filename.
            string outputPath = $"{SettingsUtil.ProjectDir}/Build/{activeTarget}";

            var buildOptions = BuildOptions.CompressWithLz4;
            var productName = PlayerSettings.productName;
            string location = $"{outputPath}/{productName}{GetTargetExt(activeTarget)}";

            var globalSettings = SettingsUtil.HybridCLRSettings;
            globalSettings.enable = false;
            AssetDatabase.SaveAssetIfDirty(globalSettings);
            AssetDatabase.Refresh();
            Debug.Log("====> Build AssetBundle");
            BuildAssetsCommand.BulidYooAssetsbundle(activeTarget);
            Debug.Log("====> Build UnityPlayer");
            BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions()
            {
                scenes = EditorBuildSettings.scenes.Select(f=>f.path).ToArray(),
                locationPathName = location,
                options = buildOptions,
                target = activeTarget,
                targetGroup = BuildPipeline.GetBuildTargetGroup(activeTarget),
            };

            var report = BuildPipeline.BuildPlayer(buildPlayerOptions);
            if (report.summary.result != UnityEditor.Build.Reporting.BuildResult.Succeeded)
            {
                Debug.LogError("打包失败");
                return;
            }
            Application.OpenURL($"file:///{outputPath}");
        }
        [Tooltip("打包可执行程序,使用HybridCLR热更;先编译热更dll,再打包yoasset热更bundle资源，最后打包可执行程序")]
        [MenuItem("Build/UnityPlayer(HybridCLR)")]
        public static void Build_UnityPlayer_HybridCLR()
        {
            BuildTarget activeTarget = EditorUserBuildSettings.activeBuildTarget;
            
            // Get filename.
            string outputPath = $"{SettingsUtil.ProjectDir}/Build/{activeTarget}";

            var globalSettings = SettingsUtil.HybridCLRSettings;
            globalSettings.enable = true;
            AssetDatabase.SaveAssetIfDirty(globalSettings);
            var buildOptions = BuildOptions.CompressWithLz4|BuildOptions.Development;

            string location = $"{outputPath}/{PlayerSettings.productName}{GetTargetExt(activeTarget)}";
            Debug.Log("HybridCLR Compile DLLs");
            BuildAssetsCommand.BuildAssembliesToAssetUpdates();
            AssetDatabase.Refresh();
            Debug.Log("====> Build AssetBundle");
            BuildAssetsCommand.BulidYooAssetsbundle(activeTarget);
            Debug.Log("====> Build UnityPlayer");
            BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions()
            {
                scenes = EditorBuildSettings.scenes.Select(f=>f.path).ToArray(),
                locationPathName = location,
                options = buildOptions,
                target = activeTarget,
                targetGroup = BuildPipeline.GetBuildTargetGroup(activeTarget),
            };

            var report = BuildPipeline.BuildPlayer(buildPlayerOptions);
            if (report.summary.result != UnityEditor.Build.Reporting.BuildResult.Succeeded)
            {
                Debug.LogError("打包失败");
                return;
            }
            Application.OpenURL($"file:///{outputPath}");
        }
    }
}
